import 'dart:async';
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter_shensu/common/model/game.dart';
import 'package:flutter_shensu/common/model/update_info.dart';
import 'package:dio/dio.dart';
import 'package:flutter_shensu/common/style/AiSuColors.dart';
import 'package:fluttertoast/fluttertoast.dart';

class HomePage extends StatefulWidget {
  static final String sName = "home";

  @override
  _HomePageState createState() {
    return _HomePageState();
  }
}

class _HomePageState extends State<StatefulWidget> {
  List<Game> _gameList = [];

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
        backgroundColor: Colors.white,
        appBar: AppBar(
          title: Text('神速加速器'),
        ),
        body: new GridView.count(
          padding: EdgeInsets.fromLTRB(0, 10, 0, 0),
          crossAxisCount: 4,
          primary: false,
          childAspectRatio: 3 / 4,
          children: _gameList.map((Game g) {
            return _getGameItemWidget(context, g);
          }).toList(),
        ));
  }

  @override
  void initState() {
    getAllGameList();
    super.initState();
  }

  Widget _getGameItemWidget(BuildContext context, Game g) {
    return new Column(
      crossAxisAlignment: CrossAxisAlignment.center,
      children: <Widget>[
        Container(
          width: 54.0,
          height: 54.0,
          decoration: BoxDecoration(
              image: DecorationImage(
                image: new NetworkImage(g.game_icon_url),
                fit: BoxFit.cover,
              ),
              borderRadius: BorderRadius.all(
                Radius.circular(15.0),
              )),
        ),
        Padding(
          padding: new EdgeInsets.fromLTRB(0, 8, 0, 0),
        ),
        Text(
          g.game_name_zh,
          textAlign: TextAlign.center,
          maxLines: 1,
          overflow: TextOverflow.ellipsis,
          style: TextStyle(fontSize: 12.0),
        ),
        ButtonTheme(
          minWidth: 30,
          height: 20,
          child: OutlineButton(
            highlightColor: Colors.transparent,
            // 按钮按下的背景颜色
            splashColor: Colors.transparent,
            // 点击时,水波动画的颜色
            highlightElevation: 0.0,
            //按下时的阴影
            onPressed: () {
              _showToast('当前游戏暂未开放');
            },
            child: Icon(
              Icons.arrow_downward,
              color: Color(AiSuColors.primarySpeedValue),
              size: 10,
            ),
            borderSide:
                new BorderSide(color: Color(AiSuColors.primarySpeedValue)),
          ),
        )
      ],
    );
  }

  void getAllGameList() async {
    var url = 'http://dl.js.ispeed.cn/Android/androidgamelist.json';
    var response = await Dio().get(url);
    if (response.statusCode == 200) {
      var resp = GameList.fromMap(response.data);
      setState(() {
        _gameList = resp.game_list;
      });
      print('get userinfo success ${response.statusCode}.');
    } else {
      print('get userinfo error ${response.statusCode}.');
    }
  }

  void getUpdateInfo() async {
    var url = 'http://dl.js.ispeed.cn/Android/updateInfo.json';

    var response = await Dio().get(url);
    if (response.statusCode == 200) {
      var resp = UpdateInfo(response.data);
      print(resp.apkUrl);
    } else {
      print('get userinfo error ${response.statusCode}.');
    }
  }

  void _showToast(var message) {
    Fluttertoast.showToast(
        msg: message, toastLength: Toast.LENGTH_SHORT, timeInSecForIos: 1);
  }
}
